package com.cupster.base_super_resource;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Map;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class DiyView extends View {


    public DiyView(Context context) {
        super(context);
    }

    public DiyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        mPaint = new Paint();
        mPath = new Path();
    }

    public DiyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DiyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttrs(context, attrs);
    }

    private Paint mPaint;
    private Path mPath;
    private int chart_x_color = Color.parseColor("#808080");
    private int chart_line_color = Color.parseColor("#6ce4d8");
    private float chart_line_width = 10.0f;
    private float space = 10.0f;
    private float zeroX = 120f;
    private float zeroY = 120f;
    private float axisY = 500f;
    private float axisX = 900f;
    private float scalX = 1.0f;//TODO 扩展：数据缩放呈现
    private float scalY = 1.0f;//TODO 扩展：数据缩放呈现
    private float[] lines = new float[]{0,0,0,0,0,0};
    private String[] xLabs = new String[]{"1月","2月","3月","4月","5月","6月"};

    private void initAttrs(Context context, AttributeSet attrs) {
        //1.获取属性数组
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DiyView);

        //2.获取属性值
        chart_x_color = typedArray.getColor(R.styleable.DiyView_chart_x_color, Color.parseColor("#404040"));
        chart_line_color = typedArray.getColor(R.styleable.DiyView_chart_line_color, Color.parseColor("#0000ee"));
        chart_line_width = typedArray.getDimension(R.styleable.DiyView_chart_line_width, 10.0f);
        space = typedArray.getDimension(R.styleable.DiyView_chart_line_space, 10.0f);
        typedArray.recycle();
        //初始化假数据
//        lines = new float[]{
//                //view的左上角为坐标系的（0，0）
//                //line1: x1：【原点x轴坐标值zeroX + 间距space +柱形宽的一半值chart_line_width/2】，y1：【原点y轴坐标值=y轴高度值axisY+y轴顶点在View坐标系的Y轴值zeroY】
//                //       x2：【原点x轴坐标值zeroX + 间距space +柱形宽的一半值chart_line_width/2】，y2：【原点y轴坐标值 - 要绘制的条形图高度-此处为假数据】
//                zeroX + space + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width / 2, axisY + zeroY - 20 * 1,
//                //line2: x1：【原点x轴坐标值zeroX + 间距space +柱形宽的一半值chart_line_width/2 +[第一个条形图的宽chart_line_width+第二个间距space] 】，y1：【原点y轴坐标值=y轴高度值axisY+y轴顶点在View坐标系的Y轴值zeroY】
//                //       x2 = x1  ,y2 = axisY+zeroY - 要绘制的高度值
//                zeroX + space + chart_line_width + space + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width + space + chart_line_width / 2, axisY + zeroY - 20 * 2,
//                zeroX + space + chart_line_width + space + chart_line_width + space + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width + space + chart_line_width + space + chart_line_width / 2, axisY + zeroY - 20 * 3,
//                zeroX + space + chart_line_width * 3 + space * 3 + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width * 3 + space * 3 + chart_line_width / 2, axisY + zeroY - 20 * 4,
//                zeroX + space + chart_line_width * 4 + space * 4 + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width * 4 + space * 4 + chart_line_width / 2, axisY + zeroY - 20 * 5,
//                zeroX + space + chart_line_width * 5 + space * 5 + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width * 5 + space * 5 + chart_line_width / 2, axisY + zeroY - 20 * 6,
//                zeroX + space + chart_line_width * 6 + space * 6 + chart_line_width / 2, axisY + zeroY, zeroX + space + chart_line_width * 6 + space * 6 + chart_line_width / 2, axisY + zeroY - 20 * 7,
//                zeroX + space * (1 + 7) + chart_line_width * 7 + chart_line_width / 2, axisY + zeroY, zeroX + space * (1 + 7) + chart_line_width * 7 + chart_line_width / 2, axisY + zeroY - 20 * 8,
//                //line9: x1：【原点x轴坐标值zeroX + 间距space * n +柱形宽的一半值chart_line_width *(n-1)+柱形宽的一半值chart_line_width/2 】，y1：【原点y轴坐标值=y轴高度值axisY+y轴顶点在View坐标系的Y轴值zeroY】
//                //       x2 = x1  ,y2 = axisY+zeroY - 要绘制的高度值
//                zeroX + space * (9) + chart_line_width * (9 - 1) + chart_line_width / 2, axisY + zeroY, zeroX + space * (9) + chart_line_width * (9 - 1) + chart_line_width / 2, axisY + zeroY - 20 * 9,
//                zeroX + space * 10 + chart_line_width * (10 - 1) + chart_line_width / 2, axisY + zeroY, zeroX + space * (10) + chart_line_width * (10 - 1) + chart_line_width / 2, axisY + zeroY - 20 * 10,
//                zeroX + space * 11 + chart_line_width * (11 - 1) + chart_line_width / 2, axisY + zeroY, zeroX + space * (11) + chart_line_width * (11 - 1) + chart_line_width / 2, axisY + zeroY - 20 * 11,
//                zeroX + space * 12 + chart_line_width * (12 - 1) + chart_line_width / 2, axisY + zeroY, zeroX + space * (12) + chart_line_width * (12 - 1) + chart_line_width / 2, axisY + zeroY - 20 * 12
//                //lineX：最终固定公式为
//                // //line9: x1：【zeroX + space * n +chart_line_width*(n-0.5f) 】，y1：【axisY+zeroY】
//                //          x2 = x1                                            ,y2 = axisY+zeroY - 要绘制的高度值
//                //          x1                                y1                         x2                                    y2
//                , zeroX + space * 13 + chart_line_width * 12.5f, axisY + zeroY, zeroX + space * 13 + chart_line_width * 12.5f, axisY + zeroY - 111
//        };
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.d("DiyView", "onAttachedToWindow");
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.d("DiyView","onMeasure");
//        //==============================================
//        //========= 设置自定义View的默认大小 ==============
//        //==============================================
//        //取出值
//        int width = getCalSize(600, widthMeasureSpec);
//        int height = getCalSize(300, heightMeasureSpec);
//
//        //TODO 处理值
//
//        //设置
//        setMeasuredDimension(width, height);
//    }
//
//    private int getCalSize(int defaultSize, int measureSpec) {
//        int calSize = defaultSize;
//
//        int mode = MeasureSpec.getMode(measureSpec);//获取模式
//        int size = MeasureSpec.getSize(measureSpec);//获取大小
//
//        switch (mode) {
//            case MeasureSpec.UNSPECIFIED: //未指定，就设置为默认大小
//                calSize = defaultSize;
//                break;
//
//            case MeasureSpec.AT_MOST: //最大取值，将大小取最大值，也可设置自定义View的mMaxHeight / mMaxWidth
//                calSize = size;
//                break;
//
//            case MeasureSpec.EXACTLY: //指定值
//                calSize = size;
//                break;
//            default:
//                break;
//        }
//        return calSize;
//    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("DiyView", "onLayout");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d("DiyView", "onDraw");

        mPaint.reset();
        mPath.reset();
        //1.画坐标轴
        mPaint.setColor(Color.parseColor("#404040"));
        mPaint.setStyle(Paint.Style.STROKE);
        mPath.moveTo(zeroX, zeroY);//y轴顶点(120,120)
        mPath.rLineTo(0, axisY);//y轴方向
        mPath.rLineTo(axisX, 0);//x轴方向
        canvas.drawPath(mPath, mPaint);//绘制
        //2.画x轴label
        mPaint.reset();
        mPaint.setColor(chart_x_color);
        mPaint.setTextSize(18);//px
        mPaint.setStyle(Paint.Style.FILL);
        //假数据
//        canvas.drawText("1月", zeroX + space, zeroY + axisY + 28, mPaint);
//        canvas.drawText("2月", zeroX + space * 2 + chart_line_width * (2 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("3月", zeroX + space * 3 + chart_line_width * (3 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("4月", zeroX + space * 4 + chart_line_width * (4 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("5月", zeroX + space * 5 + chart_line_width * (5 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("6月", zeroX + space * 6 + chart_line_width * (6 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("7月", zeroX + space * 7 + chart_line_width * (7 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("8月", zeroX + space * 8 + chart_line_width * (8 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("9月", zeroX + space * 9 + chart_line_width * (9 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("10月", zeroX + space * 10 + chart_line_width * (10 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("11月", zeroX + space * 11 + chart_line_width * (11 - 1), zeroY + axisY + 28, mPaint);
//        canvas.drawText("12月", zeroX + space * 12 + chart_line_width * (12 - 1), zeroY + axisY + 28, mPaint);
        for (int i = 0 ; i < xLabs.length ; i++){
            canvas.drawText(xLabs[i], zeroX + space*(i+1) + i*chart_line_width, zeroY + axisY + 28, mPaint);
        }
        //3.画条形图
        if (lines==null || lines.length<1){
            return;
        }
        mPaint.reset();
        mPaint.setColor(chart_line_color);
        mPaint.setStrokeWidth(chart_line_width);
        canvas.drawLines(lines, mPaint);

    }

    private int animProgress = 0;
    private ValueAnimator mAnimtor  ;
    public void startAnim(){
        if (mAnimtor == null){
            mAnimtor = ValueAnimator.ofInt(0,100);

            mAnimtor.setDuration(800);
            mAnimtor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    animProgress = (int) valueAnimator.getAnimatedValue();
                    postInvalidate();
                    if (animProgress == 100){
                        mAnimtor.removeAllUpdateListeners();
                    }
                }
            });
        }
        mAnimtor.start();
    }
    float[] arr;
    public void setHeightArr(Map<String ,Float> datas) {
        //0.判空
        if (datas == null || datas.size() < 1) {
            return;
        }
        //1.取数据
         arr = new float[datas.size()];
        xLabs = new String[datas.size()];
        int i = 0;
        for (Map.Entry entry : datas.entrySet()){
            xLabs[i] = entry.getKey()==null ? "":(String) entry.getKey();
            arr[i] = entry.getValue() ==null? 0.0f :(float) entry.getValue();
            i++;
        }
        lines = new float[arr.length * 4];
        //2.检查最大值，变更Y轴上限
        for (int n = 0; n < arr.length; n++) {
            if (arr[n] > axisY) {
                axisY = arr[n] +20;
            }
        }
        //3.装载条形图数据
        for (int n = 0; n < arr.length; n++) {
            lines[4*n]     = zeroX + space * (n+1) + chart_line_width * ((n+1) - 1 + 0.5f);
            lines[1 + 4*n] = axisY + zeroY;
            lines[2 + 4*n] = zeroX + space * (n+1) + chart_line_width * ((n+1) - 1 + 0.5f);
            lines[3 + 4*n] = axisY + zeroY - arr[n];
        }
        //通知更新View
        invalidate();
    }
}
